<?
function drizzo_all_the_tags($style = "list") {
	global $drizzo_posts;
	
	drizzo_db_connect;
	
	$tags_query = @mysql_query("SELECT `tags` FROM `$drizzo_posts` ORDER BY `ID`");
	if(!mysql_errno()) {
		while($row = @mysql_fetch_assoc($tags_query)) {
			$tags = $row['tags'];
			$tag = explode(", ", $tags);
			$tag_count = count($tag);
			for($i = 0; $i < $tag_count; $i++) {
				$tags_list[] .= $tag[$i];
			}
		}

		$tags_list_count = count($tags_list);

		sort($tags_list);

		for($i = 0; $i < $tags_list_count; $i++) {
			$prev = $i - 1;
			if($tags_list[$i] != $tags_list[$prev]) {
				$tags_string[] .= $tags_list[$i];
			}
		}

		$tags_string_count = count($tags_string);
		
		sort($tags_string);

		if($style == "list") {
			for($i = 0; $i < $tags_string_count; $i++) {
				if($i < ($tags_string_count - 1)) {
					$clean_tag = preg_replace("/-/", " ", $tags_string[$i]);
					$all_tags .= $clean_tag . ", ";
				} else {
					$clean_tag = preg_replace("/-/", " ", $tags_string[$i]);
					$all_tags .= $clean_tag;
				}
			}
		}

		if($style == "array") {
			for($i = 0; $i < $tags_string_count; $i++) {
				$all_tags[]['tag'] = $tags_string[$i];
			}
		}

		return $all_tags;

	} else {
		return false;
	}
}

function drizzo_the_tag_list($style = "plain", $limit = null) {
	global $drizzo_posts;
	
	drizzo_db_connect();
	
	$tags = drizzo_all_the_tags("array");
	$tag_no = 0;
	foreach($tags as $tag) {
		$tag_count = 0;
		$tag_lookup = @mysql_query("SELECT `ID` FROM `$drizzo_posts` WHERE `tags` LIKE '%$tag[tag]%'");
		if(!mysql_errno()) {
			while($row = @mysql_fetch_assoc($tag_lookup)) {
				$tag_count++;
			}
			$tag_array[$tag_no]['count'] = $tag_count;
		} else {
			$tag_array[$tag_no]['count'] = 0;
		}
		$tag_array[$tag_no]['tag'] = $tag['tag'];
		$tag_array[$tag_no]['clean'] = preg_replace("/-/", " ", $tag['tag']);
		$tag_no++;
	}
	
	if($limit) {
		rsort($tag_array);
		$tag_array = array_chunk($tag_array, $limit);
		$tag_array = $tag_array[0];
		if($style == "cloud") {
			$tag_no = 0;
			foreach($tag_array as $tag) {
				$tags_array[$tag_no]['tag'] = $tag['tag'];
				$tags_array[$tag_no]['count'] = $tag['count'];
				$tags_array[$tag_no]['clean'] = preg_replace("/-/", " ", $tag['tag']);
				$tag_no++;
			}
			
			$tag_array = $tags_array;
			sort($tag_array);
		}
	}
	
	if($style == "plain") {
		foreach($tag_array as $tag) {
			$tag_list .= "<span class=\"tag plain\"><a href=\"/tags/" . $tag['tag'] . "/\" title=\"" . $tag['clean'] . " (" . $tag['count'] . ")\">" . $tag['clean'] . "</a></span>";
		}
	}
	
	if($style == "comma") {
		$loop_count = 0;
		foreach($tag_array as $tag) {
			if($loop_count < count($tags_array)) {
				$tag_list .= "<span class=\"tag comma\"><a href=\"/tags/" . $tag['tag'] . "/\" title=\"" . $tag['clean'] . " (" . $tag['count'] . ")\">" . $tag['clean'] . "</a></span>, ";
			} else {
				$tag_list .= "<span class=\"tag comma\"><a href=\"/tags/" . $tag['tag'] . "/\" title=\"" . $tag['clean'] . " (" . $tag['count'] . ")\">" . $tag['clean'] . "</a></span>";
			}
			$loop_count++;
		}
	}

	if(($style == "list") || ($style == "ul")) {
		$tag_list .= "<ul id=\"tag_ul\">\n";
		foreach($tag_array as $tag) {
			$tag_list .= "\t<li class=\"tag\"><a href=\"/tags/" . $tag['tag'] . "/\" title=\"" . $tag['clean'] . " (" . $tag['count'] . ")\">" . $tag['clean'] . "</a></li>\n";
		}
		$tag_list .= "</ul>\n";
	}

	if($style == "ol") {
		$tag_list .= "<ol id=\"tag_ul\">\n";
		foreach($tag_array as $tag) {
			$tag_list .= "\t<li class=\"tag\"><a href=\"/tags/" . $tag['tag'] . "/\" title=\"" . $tag['clean'] . " (" . $tag['count'] . ")\">" . $tag['clean'] . "</a></li>\n";
		}
		$tag_list .= "</ol>\n";
	}
	
	if($style == "cloud") { // Based on tutorial @ http://www.prism-perfect.net/archive/php-tag-cloud-tutorial/
		$tag_list .= "<div id=\"tag_cloud\">\n";
		$max_size = 180;
		$min_size = 80;
		foreach($tag_array as $tag) {
			$tag_cloud[$tag['tag']] = $tag['count'];
		}
		$max_count = max(array_values($tag_cloud));
		$min_count = min(array_values($tag_cloud));
		$spread = $max_count - $min_count;
		if($spread == 0) {
			$spread = 1;
		}
		$step = ($max_size - $min_size) / $spread;
		foreach($tag_cloud as $key => $value) {
			$size = $min_size + (($value - $min_count) * $step);
			$size = ceil($size);
			$tag_list .= "<a class=\"tag\" href=\"/tags/" . $key . "\" title=\"" . preg_replace("/-/", " ", $key) . " (" . $value . ")\" style=\"font-size: " . $size . "%;\">" . $key . "</a> ";
		}
		
		$tag_list .= "</div>\n";
		
		return $tag_list;
	}

	return $tag_list;
}

function drizzo_tag_cloud($limit = 25) {
	$tag_cloud = drizzo_the_tag_list("cloud", $limit);
	return $tag_cloud;
}

function drizzo_last_tag_date($tag) {
	global $drizzo_posts;
	
	drizzo_db_connect();
	
	$lookup = @mysql_query("SELECT `posted_date` FROM `$drizzo_posts` WHERE `tags` LIKE '%$tag%' ORDER BY `posted_date` DESC LIMIT 1");
	if(!mysql_errno()) {
		$last_date = @mysql_fetch_assoc($lookup);
		$last_date = $last_date['posted_date'];
		return $last_date;
	} else {
		return false;
	}
}

function drizzo_tag_search($tag = null, $limit = 0, $offset = 0) {
	global $drizzo_posts, $drizzo_comments, $drizzo_users;
	
	if($tag == null) {
		return false;
	} else {
		if($limit == 0) {
			$the_posts_query = @mysql_query("SELECT `ID` FROM `$drizzo_posts` WHERE `tags` LIKE '%$tag%' ORDER BY `ID` DESC");
		} else {
			$the_posts_query = @mysql_query("SELECT `ID` FROM `$drizzo_posts` WHERE `tags` LIKE '%$tag%' ORDER BY `ID` DESC LIMIT $offset,$limit");
		}

		if(mysql_errno()) {
			$the_posts[] = "<p>Posts could not be retrieved.</p>";
		} else {
			while($row = mysql_fetch_array($the_posts_query)) {
				$the_posts[] = drizzo_get_the_post($row['ID']);
			}
		}
		return $the_posts;
	}	
}
?>
